package cn.iocoder.yudao.module.wecom.controller.admin.msgtemplate;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wecom.controller.admin.msgtemplate.vo.*;
import cn.iocoder.yudao.module.wecom.dal.dataobject.msgtemplate.MsgTemplateDO;
import cn.iocoder.yudao.module.wecom.service.msgtemplate.MsgTemplateService;

@Tag(name = "管理后台 - 消息模板")
@RestController
@RequestMapping("/wecom/msg-template")
@Validated
public class MsgTemplateController {

    @Resource
    private MsgTemplateService msgTemplateService;

    @PostMapping("/create")
    @Operation(summary = "创建消息模板")
    @PreAuthorize("@ss.hasPermission('wecom:msg-template:create')")
    public CommonResult<Long> createMsgTemplate(@Valid @RequestBody MsgTemplateSaveReqVO createReqVO) {
        return success(msgTemplateService.createMsgTemplate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新消息模板")
    @PreAuthorize("@ss.hasPermission('wecom:msg-template:update')")
    public CommonResult<Boolean> updateMsgTemplate(@Valid @RequestBody MsgTemplateSaveReqVO updateReqVO) {
        msgTemplateService.updateMsgTemplate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除消息模板")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wecom:msg-template:delete')")
    public CommonResult<Boolean> deleteMsgTemplate(@RequestParam("id") Long id) {
        msgTemplateService.deleteMsgTemplate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得消息模板")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wecom:msg-template:query')")
    public CommonResult<MsgTemplateRespVO> getMsgTemplate(@RequestParam("id") Long id) {
        MsgTemplateDO msgTemplate = msgTemplateService.getMsgTemplate(id);
        return success(BeanUtils.toBean(msgTemplate, MsgTemplateRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得消息模板列表")
    @PreAuthorize("@ss.hasPermission('wecom:msg-template:query')")
    public CommonResult<List<MsgTemplateRespVO>> getMsgTemplateList() {
        List<MsgTemplateDO> msgTemplate = msgTemplateService.getMsgTemplateList();
        return success(BeanUtils.toBean(msgTemplate, MsgTemplateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得消息模板分页")
    @PreAuthorize("@ss.hasPermission('wecom:msg-template:query')")
    public CommonResult<PageResult<MsgTemplateRespVO>> getMsgTemplatePage(@Valid MsgTemplatePageReqVO pageReqVO) {
        PageResult<MsgTemplateDO> pageResult = msgTemplateService.getMsgTemplatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MsgTemplateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出消息模板 Excel")
    @PreAuthorize("@ss.hasPermission('wecom:msg-template:export')")
    @OperateLog(type = EXPORT)
    public void exportMsgTemplateExcel(@Valid MsgTemplatePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MsgTemplateDO> list = msgTemplateService.getMsgTemplatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "消息模板.xls", "数据", MsgTemplateRespVO.class,
                        BeanUtils.toBean(list, MsgTemplateRespVO.class));
    }

}